package com.epam.task.web.controller;

import com.epam.task.web.entity.eZakaz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * User: zhunusbekova.arailym
 */
public class Controller extends HttpServlet {

    public static final Logger LOGGER = LoggerFactory.getLogger(Controller.class);
    private static final long serialVersionUID = 1L;

    public Controller() {
        super();

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username").toLowerCase();
        String password = request.getParameter("password");

        if (username.equals("") || password.equals("")) {
            String errorString = "You should fill in both values - username and password.";

            // Transferring parameters from one JSP to another (from login.jsp to ezakaz.jsp)
            request.setAttribute("IsNoAllowed", errorString);
            request.getRequestDispatcher("/login.jsp").forward(request, response);

        } else {
            @SuppressWarnings("unchecked")
            Map parameters = request.getParameterMap();

            // Variables to define which one of the buttons on the form of JSP has been clicked
            boolean isLoginEvent = parameters.containsKey("btnLogin");
            boolean isSendEvent = parameters.containsKey("btnSend");
            boolean isLogoutEvent = parameters.containsKey("btnLogout");

            eZakaz ezakaz = new eZakaz();

            if (isLoginEvent) {
                int result = ezakaz.login(username, password);

                if (result == 0) {
                    // Transferring parameters from one JSP to another (from Login.jsp to ezakaz.jsp)
                    request.setAttribute("Username", username);

                    request.setAttribute("Password", password);

                    request.setAttribute("UserList", ezakaz.getUsersByStore());

                    request.setAttribute("AllMessages", ezakaz.getAllOrders());

                    request.getRequestDispatcher("/ezakaz.jsp").forward(request, response);
                } else {
                    String errorString = "Wrong username or password.";

                    // Transferring parameters from one JSP to another (from Login.jsp to ezakaz.jsp)
                    request.setAttribute("IsNoAllowed", errorString);
                    request.getRequestDispatcher("/login.jsp").forward(request, response);
                }
            }

            if (isSendEvent) {
                String order = request.getParameter("outcomingmess");
                ezakaz.postOrder(order, username);

                request.setAttribute("Username", username);

                request.setAttribute("Password", password);

                request.setAttribute("UserList", ezakaz.getUsersByStore());

                request.setAttribute("AllMessages", ezakaz.getAllOrders());

                request.getRequestDispatcher("/ezakaz.jsp").forward(request, response);
            }

            if (isLogoutEvent) {
                ezakaz.logout(username, password);
            }
        }
    }
}
